package com.wingsfrog.travel.controller;

import com.wingsfrog.travel.controller.dto.request.UserInfoReqDto;
import com.wingsfrog.travel.controller.dto.response.BaseResDto;
import com.wingsfrog.travel.service.MainTaskService;
import com.wingsfrog.travel.service.TeamService;
import com.wingsfrog.travel.service.UserService;
import com.wingsfrog.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by rankin on 16/3/30.
 */
@RestController
@RequestMapping(value = "mainTasks")
public class MainTaskController {
    @Autowired
    private MainTaskService mainTaskService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeamService teamService;

    @RequestMapping(method = {RequestMethod.GET})
    public Object getMainTaskList(HttpServletRequest request) {
        return mainTaskService.getMainTasks();
    }

    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    public Object getMainTaskDetail(@PathVariable Integer id, HttpServletRequest request) throws Exception {
        return mainTaskService.getMainTaskDetailById(id, SessionUtil.getUserId());
    }

    @RequestMapping(value = "{id}/join", method = {RequestMethod.POST})
    @Transactional
    public Object joinMainTaskById(@PathVariable Integer id, HttpServletRequest request, @RequestBody(required=false) UserInfoReqDto reqDto) throws Exception {
        Integer userId = SessionUtil.getUserId();

        if (reqDto != null)userService.saveUserInfo(userId,reqDto.getBrif(),reqDto.getYearsold(),reqDto.getSex());

        teamService.joinOrCreateTeamByTaskId(userId,id);
        return new BaseResDto(0,"");
    }
}
